<?php 
namespace XFCore\traits\user;
trait Two_QrlistTrait{
    public function add_usdtqrlist($get , $post , $data){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $this->validateBasicRequirements('usdt');
        $params = array_filter($data, fn($v) => $v !== '');
        if(empty($data['qr_url'])){
            exitjson(-1,'USDT-TRC20 收款地址不可为空!');
        }
        $qr_row = $this->db->qrlistrow($data['qr_url'] , 'qr_url' , "`pid`,`type`");
        
        if($qr_row && $qr_row['pid'] == $this->uid && $qr_row['type'] == 'usdt'){
            exitjson(-1,'添加失败！您之前已经添加过此USDT收款地址');
        }
        $data = array_merge($params,$this->prepareCommonData(0, 1, $data['qr_url'] ) + ['type'=>'usdt']);
        //添加二维码
        $this->insertQrlist($data);
    }
    public function edit_usdtqrlist($get , $post , $data){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        unset($data['id']);
        
        $id = $this->input->id;
        if(empty($id)){
            exitjson(-1,'非法操作！');
        }
        $params = array_filter($data, fn($v) => $v !== '');
        if(empty($data['qr_url'])){
            exitjson(-1,'USDT-TRC20 收款地址不可为空!');
        }
        $params['cookie'] = $params['qr_url'];
        $qr_row = $this->db->qrlistrow($id , 'id', "`pid`,`id`,`type`");
        if(empty($qr_row) || $qr_row['pid'] != $this->uid){
            exitjson(-1,'数据不存在！');
        }
        $qr_row = $this->db->qrlistrow($params['qr_url'] , 'qr_url', "`pid`,`id`,`type`");
        
        if($qr_row['id'] != $id  && $qr_row['pid'] == $this->uid){
            exitjson(-1,'修改失败！您之前已经添加过此USDT收款地址');
        }
        if($this->db->update('qrlist',$params,['id'=>$id])){
            exitjson(200,'修改成功！');
        }
        exitjson(-1,'修改失败！');
    }
    
    public function add_hangqrlist($get , $post , $data){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $params = array_filter($data, fn($v) => $v !== '');
        
        $requiredFields = ['xf_type', 'type', 'qr_url'];
        $isEmpty = false;
        // 循环检查每个必填项是否为空
        foreach ($requiredFields as $field) {
            if (empty($data[$field])) {
                $isEmpty = true;
                break; // 一旦发现空值，退出循环
            }
        }
        
        if($isEmpty){
            exitjson(-1,'必填项不可为空！');
        }
        
        $this->validateBasicRequirements($params['type']);
        $unsupportedAppTypes = ['qqpay'];
        $unsupportedPcTypes = ['alipay', 'qqpay'];
        
        // 检查 APP 支持的支付类型
        if ($params['xf_type'] == 3 && in_array($params['type'], $unsupportedAppTypes)) {
            exitjson(-1, 'APP不支持 QQ支付');
        }
        
        // 检查 PC 支持的支付类型
        if ($params['xf_type'] == 1 && in_array($params['type'], $unsupportedPcTypes)) {
            exitjson(-1, 'PC通道无需添加支付宝和QQ通道');
        }

        $data = array_merge($params,$this->prepareCommonData(1, 0, ($params['xf_type'] == '3'?base64_encode('xfappguajicookie'):'')));
        $this->insertQrlist($data);
    }
    public function edit_hangqrlist($get , $post , $data){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        unset($data['id']);
        $id = $this->input->id;
        if(empty($id)){
            exitjson(-1,'非法操作！');
        }
        $qr_row = $this->db->qrlistrow($id);
        if(empty($qr_row) || $qr_row['pid'] != $this->uid){
            exitjson(-1,'数据不存在！');
        }
        $params = array_filter($data, fn($v) => $v !== '');
        if($this->db->update('qrlist',$params,['id'=>$id])){
            exitjson(200,'修改成功！');
        }
        exitjson(-1,'修改失败！');
    }
} 
?>